<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
//列表控制器
class ListController extends Controller{
	public function index(){
		$cateModel = new \Common\Model\Cate;//载入分类模板
		$tagModel = new \Common\Model\Tag;//载入标签模板
		$arcModel = new \Common\Model\Arc;//载入文章表模板
		$arcTagModel = new \Common\Model\ArcTag;//载入标签表和文章表的中间表模板
//		分类还是标签
//		判断如果是分类的时候
		if($cid = Q('get.cid',0,'intval')){
//		1.名称
		$name = '分类';
//		p($name);exit;
//		2.值
		$value = $cateModel->where("cid={$cid}")->pluck('cname');
//		p($value);exit;
//		3.文章总数
//		获得当前分类的所有子集的cid
		$cids = $cateModel->getSon($cateModel->get(),$cid);
		$cids[] = $cid;
		$where = "category_cid IN(" . implode(',', $cids) . ")";
		$total = $arcModel->where($where)->count();
//		p($total);exit;
		$data = $arcModel->join('category','category_cid','=','cid')->field('aid,title,sendtime,author,cid,cname,digest,thumb')->where($where)->get();
			foreach ($data as $k => $v) {
				$data[$k]['tag'] = $arcTagModel->join('tag','tag_tid','=','tid')->where("article_aid={$v['aid']}")->get();
			}
	}
		
		
//	判断如果是标签的时候
	if($tid = Q('get.tid',0,'intval')){
//		名称
		$name = '标签';
//		值
		$value = $tagModel->where("tid={$tid}")->pluck('tname');
//		p($value);exit;
//		文章总数
		$where = "tag_tid={$tid}";
		$total = $arcTagModel->where("tag_tid={$tid}")->count();
		//4.所有文章
		$data = $arcTagModel->join('article','article_aid','=','aid')->join('category','category_cid','=','cid')->where($where)->get();
//		p($data);
		foreach ($data as $k => $v) {
			$data[$k]['tag'] = $arcTagModel->join('tag','tag_tid','=','tid')->where("article_aid={$v['aid']}")->get();
		}
	}
	View::with('name',$name);
	View::with('value',$value);
	View::with('total',$total);
	View::with('data',$data);
	
	View::make();
	}
}


 ?>